package kaluli;
//先加入dom4j.jar包 
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**   
* @Title: TestDom4j.java
* @Package 
* @Description: 解析xml字符串
* @author 无处不在
* @date 2012-11-20 下午05:14:05
* @version V1.0   
*/
public class Dom4jUitls {

    public static String readStringXml(String xml,String type) {
        Document doc = null;
        String info="";
        try {

            // 读取并解析XML文档
            // SAXReader就是一个管道，用一个流的方式，把xml文件读出来
            // 
            // SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文档
            // Document document = reader.read(new File("User.hbm.xml"));
            // 下面的是通过解析xml字符串的
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML

            Element rootElt = doc.getRootElement(); // 获取根节点
            System.out.println("根节点：" + rootElt.getName()); // 拿到根节点的名称
            
            if(type.equals("HG")){
            	//hg
            	info = rootElt.selectSingleNode("/ResponseMessage/ResponseBodyList/ReturnInfo").getText();
            	
            }else if(type.equals("GJ")){
            	
            	info = rootElt.selectSingleNode("//Notes").getText();
            }
            
            return info.toString();
         
        } catch (DocumentException e) {
            e.printStackTrace();
            info=e.getMessage();
        } 
        return info;
    }

    /**
     * @description 将xml字符串转换成map
     * @param xml
     * @return Map
     */
    public static Map readStringXmlOut(String xml) {
        Map map = new HashMap();
        Document doc = null;
        try {
            // 将字符串转为XML
            doc = DocumentHelper.parseText(xml); 
            // 获取根节点
            Element rootElt = doc.getRootElement(); 
            // 拿到根节点的名称
            System.out.println("根节点：" + rootElt.getName()); 

            // 获取根节点下的子节点head
            Iterator iter = rootElt.elementIterator("head"); 
            // 遍历head节点
            while (iter.hasNext()) {

                Element recordEle = (Element) iter.next();
                // 拿到head节点下的子节点title值
                String title = recordEle.elementTextTrim("title"); 
                System.out.println("title:" + title);
                map.put("title", title);
                // 获取子节点head下的子节点script
                Iterator iters = recordEle.elementIterator("script"); 
                // 遍历Header节点下的Response节点
                while (iters.hasNext()) {
                    Element itemEle = (Element) iters.next();
                    // 拿到head下的子节点script下的字节点username的值
                    String username = itemEle.elementTextTrim("username"); 
                    String password = itemEle.elementTextTrim("password");

                    System.out.println("username:" + username);
                    System.out.println("password:" + password);
                    map.put("username", username);
                    map.put("password", password);
                }
            }

            //获取根节点下的子节点body
            Iterator iterss = rootElt.elementIterator("body"); 
            // 遍历body节点
            while (iterss.hasNext()) {
                Element recordEless = (Element) iterss.next();
                // 拿到body节点下的子节点result值
                String result = recordEless.elementTextTrim("result"); 
                System.out.println("result:" + result);
                // 获取子节点body下的子节点form
                Iterator itersElIterator = recordEless.elementIterator("form"); 
                // 遍历Header节点下的Response节点
                while (itersElIterator.hasNext()) {
                    Element itemEle = (Element) itersElIterator.next();
                    // 拿到body下的子节点form下的字节点banlce的值
                    String banlce = itemEle.elementTextTrim("banlce"); 
                    String subID = itemEle.elementTextTrim("subID");

                    System.out.println("banlce:" + banlce);
                    System.out.println("subID:" + subID);
                    map.put("result", result);
                    map.put("banlce", banlce);
                    map.put("subID", subID);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public static void main(String[] args) {

        // 下面是需要解析的xml字符串例子
        String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<ROOT>"+
  "<Head>"+
   " <MessageID>psp000000000000000133910028</MessageID>"+
    "<MessageType>DOCREC</MessageType>"+
    "<Sender>ICIP</Sender>"+
   " <Receiver>1500004017</Receiver>"+
    "<SendTime>2016-06-12 10:48:17</SendTime>"+
   " <FunctionCode>01</FunctionCode>"+
   " <Version>1.0</Version>"+
  "</Head>"+
  "<Declaration>"+
  "  <OrgMessageID>NSGJ201606121040034689</OrgMessageID>"+
  "  <OrgMessageType>661101</OrgMessageType>"+
   " <OrgSenderID>1500004017</OrgSenderID>"+
  "  <OrgReceiverID>ICIP</OrgReceiverID>"+
  "  <OrgRecTime>2016-06-12 10:40:03.085</OrgRecTime>"+
   " <Status>20</Status>"+
   " <Notes>电商企业1500004240的订单号为KLL20160612058100434044023订单数据已存在！</Notes>"+
 " </Declaration>"+
"</ROOT>";
        String str = readStringXml(xmlString, "HG");
        System.out.println(str);
    
    }

}